﻿@using Ext.Net
@{
    ViewBag.Title = "Permission List";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@(Html.X()
     .Viewport()
     .Layout(LayoutType.Border)
     .Items(view =>
                {
                    view.Add(Html.X()
                                 .Panel()
                                 .Region(Region.West)
                                 .Title("Permission List")
                                 .Width(320)
                                 .Split(true)
                                 .Layout(LayoutType.Fit)
                                 .Items(c => c.Add(Html.X()
                                                       .GridPanel()
                                                       .Scroll(ScrollMode.Both)
                                                       .ForceFit(true)
                                                       .MultiSelect(true)
                                                       .SelectionModel(Html.X()
                                                                           .CheckboxSelectionModel()
                                                                           .Mode(SelectionMode.Multi))
                                                       .ID("gvOutList")
                                                       .ColumnModel(v =>
                                                                        {
                                                                            v.Columns.Add(Html.X()
                                                                                              .Column()
                                                                                              .DataIndex("GroupName")
                                                                                              .Text("GroupName"));
                                                                            v.Columns.Add(Html.X()
                                                                                              .Column()
                                                                                              .DataIndex("Name")
                                                                                              .Text("Name"));
                                                                        })
                                                       .Store(Html.X()
                                                                  .Store()
                                                                  .AutoLoad(true)
                                                                  .RemoteSort(true)
                                                                  .RemotePaging(false)
                                                                  .RemoteGroup(true)
                                                                  .PageSize(int.MaxValue)
                                                                  .AutoSync(false)
                                                                  .Model(Html.X()
                                                                             .Model()
                                                                             .Fields("Id",
                                                                                     "Name",
                                                                                     "GroupName"))
                                                                  .Proxy(Html.X()
                                                                             .AjaxProxy()
                                                                             .Json(true)
                                                                             .Url(Url.Action("PermissionOutList",
                                                                                             new
                                                                                                 {
                                                                                                         id = ViewBag.Id
                                                                                                 }))
                                                                             .ActionMethods(v => v.Read = HttpMethod.POST)
                                                                             .Reader(Html.X()
                                                                                         .JsonReader()
                                                                                         .Root("data")
                                                                                         .TotalProperty("total"))))
                                                       .Features(v => v.Add(Html.X()
                                                                                .Grouping()
                                                                                .GroupHeaderTplString("{name} ({[values.rows.length]} {[values.rows.length > 1 ? \"items\" : \"item\"]})"))))));
                    view.Add(Html.X()
                                 .Panel()
                                 .Region(Region.East)
                                 .Title("Permission In List")
                                 .Width(315)
                                 .Split(true)
                                 .Layout(LayoutType.Fit)
                                 .Items(c => c.Add(Html.X()
                                                       .GridPanel()
                                                       .Scroll(ScrollMode.Both)
                                                       .ForceFit(true)
                                                       .MultiSelect(true)
                                                       .SelectionModel(Html.X()
                                                                           .CheckboxSelectionModel()
                                                                           .Mode(SelectionMode.Multi))
                                                       .ID("gvInList")
                                                       .ColumnModel(v =>
                                                                        {
                                                                            v.Columns.Add(Html.X()
                                                                                             .Column()
                                                                                             .DataIndex("GroupName")
                                                                                             .Text("Group Name"));
                                                                            v.Columns.Add(Html.X()
                                                                                              .Column()
                                                                                              .DataIndex("Name")
                                                                                              .Text("Name"));
                                                                        })
                                                       .Store(Html.X()
                                                                  .Store()
                                                                  .AutoLoad(true)
                                                                  .RemoteSort(true)
                                                                  .RemotePaging(false)
                                                                  .RemoteGroup(true)
                                                                  .PageSize(int.MaxValue)
                                                                  .AutoSync(false)
                                                                  .Model(Html.X()
                                                                             .Model()
                                                                             .Fields("Id",
                                                                                     "Name",
                                                                                     "GroupName"))
                                                                  .Proxy(Html.X()
                                                                             .AjaxProxy()
                                                                             .Json(true)
                                                                             .Url(Url.Action("PermissionInList",
                                                                                             new
                                                                                                 {
                                                                                                         id = ViewBag.Id
                                                                                                 }))
                                                                             .ActionMethods(v => v.Read = HttpMethod.POST)
                                                                             .Reader(Html.X()
                                                                                         .JsonReader()
                                                                                         .Root("data")
                                                                                         .TotalProperty("total"))))
                                                       .Features(v => v.Add(Html.X()
                                                                                .Grouping()
                                                                                .GroupHeaderTplString("{name} ({[values.rows.length]} {[values.rows.length > 1 ? \"items\" : \"item\"]})"))))));
                    view.Add(Html.X()
                                 .Panel()
                                 .Region(Region.Center)
                                 .Layout(LayoutType.VBox)
                                 .Items(panel =>
                                            {
                                                panel.Add(Html.X()
                                                              .Button()
                                                              .Text(">>")
                                                              .MarginSpec("80 5 10 5")
                                                              .OnClientClick("addPermission"));
                                                panel.Add(Html.X()
                                                              .Button()
                                                              .Text("<<")
                                                              .MarginSpec("0 5")
                                                              .OnClientClick("removePermission"));
                                            }));
                }))
@section script
{
    <script type="text/javascript">
        function addPermission() {
            var model = window.App.gvOutList.getSelectionModel();
            if (model.selected.length == 0)
                window.Ext.Msg.show(
                    {
                        title: "Confirm",
                        buttons: window.Ext.Msg.YES,
                        icon: window.Ext.Msg.ERROR,
                        msg: "Please choose an item in list."
                    });
            else {
                var ids = [];
                for (var i = 0; i < model.selected.items.length; i++)
                    ids.push(model.selected.items[i].data.Id);
                $.post('@Url.Action("AddPermission")', { rid: '@ViewBag.Id', id: ids.toString() }, function(data) {
                    window.App.gvOutList.getStore().reload();
                    window.App.gvInList.getStore().reload();
                });
            }
        }

        function removePermission() {
            var model = window.App.gvInList.getSelectionModel();
            if (model.selected.length == 0)
                window.Ext.Msg.show(
                    {
                        title: "Confirm",
                        buttons: window.Ext.Msg.YES,
                        icon: window.Ext.Msg.ERROR,
                        msg: "Please choose an item in list."
                    });
            else {
                var ids = [];
                for (var i = 0; i < model.selected.items.length; i++)
                    ids.push(model.selected.items[i].data.Id);
                $.post('@Url.Action("RemovePermission")', { rid: '@ViewBag.Id', id: ids.toString() }, function(data) {
                    window.App.gvOutList.getStore().reload();
                    window.App.gvInList.getStore().reload();
                });
            }
        }
    </script>
}